﻿using Sl.Upload.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sl.Upload.Models;
using Sl.Upload.Help;

namespace Sl.Upload
{
    public class ImageUpload : IUpload
    {
        public CompleteInfo Upload(FileInfoModel fim)
        {
            CompleteInfo ci = new CompleteInfo();
            OtherHelp.CreateDirectory(fim);
            ImageHelp img = new ImageHelp();
            if (img.SaveImage(fim.StreamInfo, fim.SavePath + "\\" + fim.UploadName))
            {
                if (fim.CutItem != null && fim.CutItem.Count > 0)
                {
                    foreach (CutInfo w in fim.CutItem)
                    {
                        w.GeneratePath = w.GeneratePath + "\\" + fim.UploadName;
                        img.ToThumImages(fim.StreamInfo, w);
                    }
                }
                ci.Name = fim.OriginalName;
                ci.Url = fim.UrlPath + "/" + fim.UploadName;
                ci.Size = fim.ContentLength;
                ci.UploadStatus = UploadStatus.Success;
                ci.Suffix = fim.FileSuffix;
                ci.Height = System.Drawing.Image.FromStream(fim.StreamInfo).Height;
                ci.Width = System.Drawing.Image.FromStream(fim.StreamInfo).Width;
            }
            else
            {
                ci.UploadStatus = UploadStatus.Error;
            }
            return ci;
        }
    }
}
